import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

public class Studio implements Runnable {
	private static final int MINUTES = 60 * 24;

	private void solve() {

		Vertex source = new Vertex(-1);
		Vertex sink = new Vertex(-1);

		Vertex[] times = new Vertex[MINUTES];
		for (int i = 0; i < times.length; i++) {
			times[i] = new Vertex(-1);
		}
		int n = nextInt();

		int start = nextTime();
		int finish = nextTime();
		for (int i = start; i < finish; i++) {
			source.addEdge(times[i], 1, 0);
		}

		Vertex[] rooms = new Vertex[n];
		for (int i = 0; i < n; i++) {
			rooms[i] = new Vertex(i);
		}

		int s1 = 0;
		for (int i = 0; i < n; i++) {
			int c1 = nextInt();
			s1 += c1;
			rooms[i].addEdge(sink, c1, 0);
			int k = nextInt();
			int[] z = new int[MINUTES];
			for (int j = 0; j < k; j++) {
				int st = nextTime();
				int fn = nextTime();
				for (int t = st; t < fn; t++) {
					z[t]++;
				}
			}
			int l = nextInt();
			boolean[] zz = new boolean[MINUTES];
			for (int j = 0; j < l; j++) {
				int st = nextTime();
				int fn = nextTime();
				int c = nextInt();
				Vertex lim = new Vertex(i);
				for (int t = st; t < fn; t++) {
					zz[t] = true;
					times[t].addEdge(lim, 1, z[t]);
				}
				lim.addEdge(rooms[i], c, 0);
			}
			for (int t = 0; t < MINUTES; t++) {
				if (!zz[t]) {
					times[t].addEdge(rooms[i], 1, z[t]);
				}
			}
		}

		int res = 0;
		int s = 0;
		Deque<Vertex> d = new ArrayDeque<Vertex>(all.size());
		while (true) {
			d.clear();
			for (Vertex vertex : all) {
				vertex.state = 0;
				vertex.d = Integer.MAX_VALUE;
				vertex.p = null;
			}
			source.d = 0;
			source.state = 1;
			d.add(source);
			while (!d.isEmpty()) {
				Vertex v = d.removeFirst();
				v.state = 2;
				for (Edge edge : v.e)
					if (edge.c > 0) {
						Vertex u = edge.dst;
						if (u.d > v.d + edge.cost) {
							u.d = v.d + edge.cost;
							u.p = edge;
							if (u.state == 0) {
								d.addLast(u);
							} else if (u.state == 2) {
								d.addFirst(u);
							}
							u.state = 1;
						}
					}
			}
			if (sink.d < Integer.MAX_VALUE) {
				res += sink.d;
				s++;
				Edge e = sink.p;
				while (e != null) {
					e.c--;
					e.rev.c++;
					e = e.src.p;
				}
			} else {
				break;
			}
		}
		if (s < s1) {
			writer.println("NO");
		} else {
			writer.println(res);
			int[] bz = new int[MINUTES + 1];
			Arrays.fill(bz, -1);
			for (int i = 0; i < times.length; i++) {
				Vertex time = times[i];
				for (Edge edge : time.e) {
					if (edge.dst.room >= 0 && edge.c == 0) {
						if (bz[i] != -1)
							throw new RuntimeException("!!!");
						bz[i] = edge.dst.room;
					}
				}
			}
			for (int i = 0; i < n; i++) {
				int m = 0;
				for (int t = 0; t < MINUTES; t++) {
					if (bz[t] == i && (t == 0 || bz[t - 1] != i)) {
						m++;
					}
				}
				writer.println();
				writer.println(m);
				for (int t = 0; t < MINUTES; t++) {
					if (bz[t] == i && (t == 0 || bz[t - 1] != i)) {
						writer.print(toTime(t) + " ");
					}
					if (bz[t] == i && bz[t + 1] != i) {
						writer.println(toTime(t + 1));
					}
				}

			}

		}

	}

	private String toTime(int t) {
		return "" + (t / 600) + (t % 600 / 60) + ":" +
				(t % 60 / 10) + (t % 10);

	}

	List<Vertex> all = new ArrayList<Vertex>();

	class Vertex {
		public int state;
		public int d;
		public Edge p;

		int room;

		List<Edge> e = new ArrayList<Edge>();

		Vertex(int room) {
			all.add(this);
			this.room = room;
		}

		public void addEdge(Vertex v, int c, int cost) {
			Edge e1 = new Edge(this, v, c, cost);
			Edge e2 = new Edge(v, this, 0, -cost);
			e1.rev = e2;
			e2.rev = e1;
			this.e.add(e1);
			v.e.add(e2);
		}
	}

	class Edge {
		Vertex src;
		Vertex dst;
		int c;
		int cost;
		Edge rev;

		Edge(Vertex src, Vertex dst, int c, int cost) {
			this.src = src;
			this.dst = dst;
			this.c = c;
			this.cost = cost;
		}
	}

	public static void main(String[] args) {
		new Studio().run();
	}

	BufferedReader reader;
	StringTokenizer tokenizer;
	PrintWriter writer;

	static final String TASK_ID = "studio";

	public void run() {
		try {
			reader = new BufferedReader(new FileReader(TASK_ID + ".in"));
			tokenizer = null;
			writer = new PrintWriter(TASK_ID + ".out");
			solve();
			reader.close();
			writer.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	int nextTime() {
		String s = nextToken();
		return
				(s.charAt(0) - '0') * 600 +
						(s.charAt(1) - '0') * 60 +
						(s.charAt(3) - '0') * 10 +
						(s.charAt(4) - '0');
	}

	int nextInt() {
		return Integer.parseInt(nextToken());
	}

	long nextLong() {
		return Long.parseLong(nextToken());
	}

	double nextDouble() {
		return Double.parseDouble(nextToken());
	}

	String nextToken() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		String s = tokenizer.nextToken();
//		System.err.println(s);
		return s;
	}
}
